<?php
namespace App\Http\Controllers\Agent;
use App\Http\Controllers\AgentController;
use App\Models\FeeGood;
use App\Models\Good;
use App\Models\Order as TOrder;
use Illuminate\Http\Request;
use App\Models\UpLog;
use App\Models\Agent;

class Order extends AgentController{
    public function index(Request $req){
        $page = $req->post('page',1);
        $status = $req->post('status',0);
        $type = $req->post('type',0);
        $kword = $req->post('kword','');
        $size = 5;
        $list = TOrder::select('ordersn','price','createtime','status','nums','aid','reback','paid','preback','reason','type','gid','param');
        if ($status == 3) $list->where('status',-1);
        elseif ($status > 0) $list->where('status',$status);
        else $list->where('status','!=',0);
        if ($kword) $list->where("ordersn",$kword);
        $nlist = clone $list;
        $nlist->where('paid',$req->agent->id);
        $list->where('aid',$req->agent->id);
        if ($type > 0) {
            $list->where('wid',0);
            $nlist->where('pwid',0);
            if ($type == 2) {//可提
                $list->where('status',2);
                $nlist->where('status',2);
                $uporder = UpLog::selectRaw("'' as ordersn,'' as price,createtime,2 as status,'' as nums,aid,0 as reback,paid,credit as preback,'' as reason,'up' as type,0 as gid,'' as param")
                                                ->where('paid',$req->agent->id)->where('wid',0);
                $list->union($uporder);
            }else{//待结算
                $list->where('status',1);
                $nlist->where('status',1);
            }
        }
        $list->union($nlist)->orderByDesc('createtime')->offset(($page-1)*$size)->limit($size);
        $orders = [];
        foreach ($list->get() as $value) {
            $o = array('ordersn'=>'单号: '.$value->ordersn,'price'=>$value->price,'createtime'=>date('Y-m-d H:i',$value->createtime),'status'=>$value->status,'nums'=>$value->nums);
            if ($value['status'] > 0) {
                if ($value->aid == $req->agent->id) $o['reback'] = round($value->reback,2);
                elseif ($value->paid == $req->agent->id) $o['reback'] = round($value->preback,2);
            }
            if ($value->status == -1) $o['statusstr'] = '已退款';
            elseif ($value->status == 1) $o['statusstr'] = '进行中';
            elseif ($value->status == 2) $o['statusstr'] = '已完成';
            else $o['statusstr'] = '未支付';
            if ($value->reason) $o['reason'] = $value->reason;
            $o['good'] = [];
            if ($value->type == 'up') {
                unset($o['good']);
                $o['ordersn'] = Agent::where('id',$value->aid)->value('mobile');
                $o['statusstr'] = '升级等级';
            }elseif ($value->type < 10) {
                if ($value->type == 0) {
                    $o['good']['title'] = '话费';
                    $o['good']['logo'] = '/static/hf_icon.jpeg';
                }elseif ($value->type == 1) {
                    $o['good']['title'] = '电费';
                    $o['good']['logo'] = '/static/df_icon.png';
                }elseif ($value->type == 2) {
                    $o['good']['title'] = '燃气';
                    $o['good']['logo'] = '/static/rq_icon.png';
                }
                $o['good']['optiontitle'] = FeeGood::where('id',$value->gid)->value('title').'元';
            }else{
                $g = Good::find($value->gid);
                $o['good']['title'] = $g->title;
                $o['good']['logo'] = getImage($g->logo,1);
                $p = json_decode($value->param,1);
                $o['good']['optiontitle'] =$p['title'];
            }
            $orders[] = $o;
        }
        wxresp(1,['list'=>$orders]);
    }
    
}